local numberListModule = {}

function numberListModule.createNumberList( parent )

	-- to do : image의 변경 등이 필요하다면 관련 설정을 table로 받도록 Refactoring 하자.

	local textureName = "Number.png"
	local frameWidth = 16
	local frameHeight = 40
	local frames = {}
	for i = 1, 10 do
		frames[i] = { x = 0, y = frameHeight * (i-1), width = frameWidth, height = frameHeight }
	end
	
	local numberList = { view = display.newGroup(), list = {}, imageSheet = graphics.newImageSheet( textureName, { frames = frames } ) }
	parent:insert( numberList.view )

	function numberList:createNumber( parent, imageSheet )
		local sprite = display.newSprite( imageSheet, { name = "num", start = 1, count = 10, time = 1000} )
		parent:insert( sprite )
		
		local numberObj = { view = sprite }
		
		function numberObj:SetView( Num )
			numberObj.view:setFrame( Num + 1 )
		end
		
		return numberObj
	end
	
	function numberList:resize( size )
		if size > #self.list then
			local needAmount = size - #self.list
			for i = #self.list, needAmount do
				self.list[#self.list + 1] = self:createNumber( self.view, self.imageSheet )
			end
		end
	end
	
	function numberList:setNumber( num )
		local stringScore = tostring( num )
		self:resize( #stringScore )

		for i = 1, #self.list do
			self.list[i].view.isVisible = false
		end
		
		for i = 1, #stringScore do
			self.list[i].view.isVisible = true
			self.list[i]:SetView( tonumber(stringScore:sub(i, i)) )
			self.list[i].view.x = display.contentCenterX + ((i - 1) * frameWidth)
			self.list[i].view.y = display.contentCenterY
		end
	end
	
	return numberList
end

return numberListModule;